
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Text
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for add_content_item.
	''' </summary>
	Public Partial Class add_content_item
		Inherits BaseWebPage

		Protected id As Integer = -1
		' Left Element ID
		Protected pageTypeID As Integer = -1
		' Special page ID
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' this.pageTemplate.PageTitle = PageTitles.ADD_CONTENT_ITEM_PAGE;

			Dim user As User = Me.LoggedUser
			If user = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.ADD_CONTENT_ITEM, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If user.Status <> Constants.appAdmin Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			If Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = Nothing OrElse Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			If Request.Params("id") = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			pageTypeID = Convert.ToInt32(Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM))
			id = Convert.ToInt32(Request.Params("id"))

			Dim tmpSpecialPage As SpecialPage = SpecialPages.GetSpecialPageByID(pageTypeID)
			lblPageName.Text = tmpSpecialPage.Name

			'init column names
			lblMidColumnName.Text = (Me.GetPageConstText(htPageConstants, "CONST_ContentColumnName2Name_TEXT")).Replace("[PARAM]", tmpSpecialPage.ContentColumnName2)
			lblMidColumnURL.Text = (Me.GetPageConstText(htPageConstants, "CONST_ContentColumnName2URL_TEXT")).Replace("[PARAM]", tmpSpecialPage.ContentColumnName2)
			lblMidColumnLinkType.Text = (Me.GetPageConstText(htPageConstants, "CONST_ContentColumnName2LinkType_TEXT")).Replace("[PARAM]", tmpSpecialPage.ContentColumnName2)

			lblThirdColumn.Text = tmpSpecialPage.ContentColumnName3 + " - What we think"

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				trGroupRow.Visible = False
				cvUserGroup.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_VALIDATOR_MSG")
			Else
				trGroupRow.Visible = True
				ddlUsers.Attributes.Add("onchange", "document.forms[0]['" + ddlGroups.UniqueID + "'].selectedIndex=0")
				ddlGroups.Attributes.Add("onchange", "document.forms[0]['" + ddlUsers.UniqueID + "'].selectedIndex=0")

				cvUserGroup.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_VALIDATOR_MSG_BOTH")
			End If

			If Constants.SHOW_MUSIC <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			If Not Me.IsPostBack Then
				Dim contentItem As New ContentItem()

				' init Link Types Drop-down
				Dim i As Integer
				Dim linkTypesCount As Integer = Constants.LINKTYPES.List.Count
				Dim tmpLinkType As BO.LinkTypes.LinkType

				ddlLinkType.Items.Add("Choose link type")
				ddlLinkType.Items.FindByText("Choose link type").Value = ""

				Dim tmpLI As ListItem
				i = 0
				While i < linkTypesCount
					tmpLinkType = DirectCast(Constants.LINKTYPES.List(i), LinkTypes.LinkType)
					tmpLI = New ListItem(tmpLinkType.Name, tmpLinkType.ID.ToString())
					ddlLinkType.Items.Add(tmpLI)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				' init users list

				ddlUsers.Items.Add("Choose user")
				ddlUsers.Items.FindByText("Choose user").Value = ""

				Dim tmpUsrList As ArrayList = BO.SpecialPage.GetSpecialUsers(pageTypeID)
				Dim tmpUsr As BO.SpecialUser = Nothing
				Dim tmpUsrCount As Integer = tmpUsrList.Count

				i = 0
				While i < tmpUsrCount
					tmpUsr = DirectCast(tmpUsrList(i), BO.SpecialUser)
					tmpLI = New ListItem(tmpUsr.Name + " " + tmpUsr.LastName, tmpUsr.ID.ToString())
					ddlUsers.Items.Add(tmpLI)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				' init groups list

				ddlGroups.Items.Add("Choose group")
				ddlGroups.Items.FindByText("Choose group").Value = ""

				Dim tmpGrpList As ArrayList = BO.SpecialPage.GetSpecialGroups(pageTypeID)
				Dim tmpGrp As BO.SpecialGroup = Nothing
				Dim tmpGrpCount As Integer = tmpGrpList.Count
				i = 0
				While i < tmpGrpCount
					tmpGrp = DirectCast(tmpGrpList(i), BO.SpecialGroup)
					tmpLI = New ListItem(tmpGrp.Name, tmpGrp.ID.ToString())
					ddlGroups.Items.Add(tmpLI)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				If id <> 0 Then
					contentItem = ContentItem.GetContentItemByID(id)
					txtContentColumnName.Text = contentItem.LinkName
					txtLeftColumnURL.Text = contentItem.LinkURL

					If contentItem.LinkTypeID <> -1 Then
						ddlLinkType.ClearSelection()
						ddlLinkType.Items.FindByValue(contentItem.LinkTypeID.ToString()).Selected = True
					End If

					If contentItem.UserID > 0 Then
						If Not SpecialUser.IsMemeber(contentItem.UserID, contentItem.SpecialPageID) Then
							Dim tmpSelectedUsr As BO.User = DirectCast(Users.GetUserByID(contentItem.UserID), BO.User)
							tmpLI = New ListItem(tmpSelectedUsr.Name + " " + tmpSelectedUsr.LastName, contentItem.UserID.ToString())
							ddlUsers.Items.Add(tmpLI)
						End If
						ddlUsers.ClearSelection()
						ddlUsers.Items.FindByValue(contentItem.UserID.ToString()).Selected = True
					End If

					If contentItem.GroupID > 0 Then
						ddlGroups.ClearSelection()
						ddlGroups.Items.FindByValue(contentItem.GroupID.ToString()).Selected = True
					End If

					txtContent.Text = contentItem.Content
					txtContent.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_LEFT_ITEM_CONTENT_MAX_CHARS.ToString()))
					txtContent.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_LEFT_ITEM_CONTENT_MAX_CHARS.ToString()))
				End If
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvUserGroup.ServerValidate, AddressOf Me.cvUserGroup_ServerValidate
			AddHandler Me.cvLinkType.ServerValidate, AddressOf Me.cvLinkType_ServerValidate

		End Sub
#End Region

		Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Me.IsValid Then
				Dim sLCName As String = Request.Params(txtContentColumnName.UniqueID).Trim()
				Dim sLCURL As String = Request.Params(txtLeftColumnURL.UniqueID).Trim()
				Dim nLCLinkType As Integer = -1
				If ddlLinkType.SelectedIndex <> 0 Then
					nLCLinkType = Convert.ToInt32(Request.Params(ddlLinkType.UniqueID).Trim())
				End If
				Dim sContent As String = Request.Params(txtContent.UniqueID).Trim()
				Dim nUserID As Integer = -1
				Dim nGroupID As Integer = -1
				If Request.Params(ddlUsers.UniqueID).Trim() <> "" Then
					nUserID = Convert.ToInt32(Request.Params(ddlUsers.UniqueID).Trim())
				End If
				If (Constants.SHOW_GROUPS = Constants.SHOW_ON) AndAlso (Request.Params(ddlGroups.UniqueID).Trim() <> "") Then
					nGroupID = Convert.ToInt32(Request.Params(ddlGroups.UniqueID).Trim())
				End If

				Dim tmpSpecialPage As New SpecialPage()
				Dim tmpContentItem As New ContentItem()
				tmpContentItem.ID = id
				tmpContentItem.SpecialPageID = pageTypeID
				If nUserID <> -1 Then
					tmpContentItem.UserID = nUserID
				End If
				If nGroupID <> -1 Then
					tmpContentItem.GroupID = nGroupID
				End If
				tmpContentItem.LinkName = sLCName.Substring(0, Math.Min(sLCName.Length, Constants.TEXT_FIELD_LEFT_ITEM_LINK_NAME_MAX_CHARS))
				tmpContentItem.LinkURL = sLCURL.Substring(0, Math.Min(sLCURL.Length, Constants.TEXT_FIELD_LEFT_ITEM_LINK_URL_MAX_CHARS))
				If nLCLinkType <> -1 Then
					tmpContentItem.LinkTypeID = nLCLinkType
				End If
				tmpContentItem.Content = sContent.Substring(0, Math.Min(sContent.Length, Constants.TEXT_AREA_LEFT_ITEM_CONTENT_MAX_CHARS))

				tmpContentItem.Save()
				Response.Redirect(Constants.SPECIAL + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + pageTypeID)
			End If
		End Sub

		Private Sub cvUserGroup_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			If (ddlUsers.SelectedIndex > 0) OrElse ((Constants.SHOW_GROUPS = Constants.SHOW_ON) AndAlso (ddlGroups.SelectedIndex > 0)) Then
				args.IsValid = True
			Else
				args.IsValid = False
			End If
		End Sub

		Private Sub cvLinkType_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			If txtLeftColumnURL.Text <> "" AndAlso ddlLinkType.SelectedIndex = 0 Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






